using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;


namespace MOSS_Deploment_Wizard
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                string path = textBox1.Text;
                DirectoryInfo diFiles = new DirectoryInfo(path);
                if (diFiles.Exists == false)
                {
                    MessageBox.Show("I can't find the path specified, look somewhere else?");
                }
                else
                {
                    lbPackages.DataSource = diFiles.GetFiles("*.wsp");

                }
            }
            catch(Exception errpath)
            {
                MessageBox.Show(errpath.Message.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {// Runs the add solution commands in STSADM.EXE then puts the concatenated command into the log
                int versionPath = comboBox1.SelectedIndex;
                switch (versionPath)
                {
                    case 0:
                        {
                            versionPath = 60;
                            break;
                        }
                    case 1:
                        {
                            versionPath = 12;
                            break;
                        }
                    case 2:
                        {
                            versionPath = 14;
                            break;
                        }
                }

                if (checkBox1.Checked == true)
                {
                    List<string> items = new List<string>();
                    foreach (var s in lbPackages.Items)
                    {
                        items.Add(s.ToString());
                    }

                    foreach (string strcol in items)
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\BIN\stsadm.exe", @" -o deploysolution -name " + strcol + " -local -allowgacdeployment");
                        Console.ReadLine();                        

                    }

                }
                else
                {

                    System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\BIN\stsadm.exe", @" -o deploysolution -name " + lbPackages.SelectedItem + " -local -allowgacdeployment");
                    Console.ReadLine();
                }

            }
            catch (Exception adde)
            {
                MessageBox.Show(adde.Message.ToString());
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                // Runs the retract solution commands in STSADM.EXE
                int versionPath = comboBox1.SelectedIndex;
                switch (versionPath)
                {
                    case 0:
                        {
                            versionPath = 60;
                            break;
                        }
                    case 1:
                        {
                            versionPath = 12;
                            break;
                        }
                    case 2:
                        {
                            versionPath = 14;
                            break;
                        }
                }
                System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\bin\stsadm.exe", @" -o retractsolution -name " + lbPackages.SelectedItem + " -immediate");
                Console.ReadLine();
            }
            catch(Exception retracte)
            {
                MessageBox.Show(retracte.Message.ToString());
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // Runs the delete solution commands in STSADM.EXE
            int versionPath = comboBox1.SelectedIndex;
            switch (versionPath)
            {
                case 0:
                    {
                        versionPath = 60;
                        break;
                    }
                case 1:
                    {
                        versionPath = 12;
                        break;
                    }
                case 2:
                    {
                        versionPath = 14;
                        break;
                    }
            }

            System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\BIN\stsadm.exe", @" -o deletesolution -name " + lbPackages.SelectedItem);
            Console.ReadLine();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Exits the application
            Application.Exit();
        }

        private void retractToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Runs the add solution commands in STSADM.EXE then puts the concatenated command into the log
            int versionPath = comboBox1.SelectedIndex;
            switch (versionPath)
            {
                case 0:
                    {
                        versionPath = 60;
                        break;
                    }
                case 1:
                    {
                        versionPath = 12;
                        break;
                    }
                case 2:
                    {
                        versionPath = 14;
                        break;
                    }
            }

            System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\BIN\stsadm.exe", @" -o addsolution -filename " + textBox1.Text + "\\" + lbPackages.SelectedItem);
            Console.ReadLine();
        }

        private void retractToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            // Runs the retract solution commands in STSADM.EXE
            int versionPath = comboBox1.SelectedIndex;
            switch (versionPath)
            {
                case 0:
                    {
                        versionPath = 60;
                        break;
                    }
                case 1:
                    {
                        versionPath = 12;
                        break;
                    }
                case 2:
                    {
                        versionPath = 14;
                        break;
                    }
            }

            System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\BIN\stsadm.exe", @" -o retractsolution -name " + lbPackages.SelectedItem + " -immediate");
            Console.ReadLine();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Runs the delete solution commands in STSADM.EXE
            int versionPath = comboBox1.SelectedIndex;
            switch (versionPath)
            {
                case 0:
                    {
                        versionPath = 60;
                        break;
                    }
                case 1:
                    {
                        versionPath = 12;
                        break;
                    }
                case 2:
                    {
                        versionPath = 14;
                        break;
                    }
            }

            System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\BIN\stsadm.exe", @" -o deletesolution -name " + lbPackages.SelectedItem);
            Console.ReadLine();
        }

        private void deploySolutionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Deploys the selected solution and forces it to the GAC (-allowgacdeployment)
            
            int versionPath = comboBox1.SelectedIndex;
            switch (versionPath)
            {
                case 0:
                    {
                        versionPath = 60;
                        break;
                    }
                case 1:
                    {
                        versionPath = 12;
                        break;
                    }
                case 2:
                    {
                        versionPath = 14;
                        break;
                    }
            }

            System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\" + versionPath + @"\BIN\stsadm.exe", @" -o deploysolution -name " + lbPackages.SelectedItem + " -local -allowgacdeployment");
            Console.ReadLine();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("SharePoint Portal Server 2003");
            comboBox1.Items.Add("Microsoft Office SharePoint Server 2007");
            comboBox1.Items.Add("SharePoint Server 2010");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int versionPath = comboBox1.SelectedIndex;
            switch (versionPath)
            {
                case 0:
                    {
                        versionPath = 60;
                        break;
                    }
                case 1:
                    {
                        versionPath = 12;
                        break;
                    }
                case 2:
                    {
                        versionPath = 14;
                        break;
                    }
            }

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Written by Duane Wolff, go to http://deploymentwizard.codeplex.com for updates");
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"C:\Users\%USERNAME%\Desktop\help.txt");
        }

        private void switchSharePointVersionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // This form will list all the content databases in the selected web app.
            Form2 newform = new Form2();
            newform.Show();
        }

    }
}